Completed
Push — master ( 77b412...0a81cb )
by Sander
04:08
created

angular.controller(ꞌVaultCtrlꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
(function () {
24
	'use strict';
25
26
	/**
27
	 * @ngdoc function
28
	 * @name passmanApp.controller:MainCtrl
29
	 * @description
30
	 * # MainCtrl
31
	 * Controller of the passmanApp
32
	 */
33
	angular.module('passmanApp')
34
		.controller('VaultCtrl', ['$scope', 'VaultService', 'SettingsService', 'CredentialService', '$location', 'ShareService', 'EncryptService', '$translate', '$rootScope', function ($scope, VaultService, SettingsService, CredentialService, $location, ShareService, EncryptService, $translate, $rootScope) {
35
			VaultService.getVaults().then(function (vaults) {
36
				$scope.vaults = vaults;
37
				if (SettingsService.getSetting('defaultVault') != null) {
0 ignored issues
show
Coding Style introduced by
It is recommended to use !== to compare with null.

Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.

Read more about comparison operations.

Loading history...
38
					var default_vault = SettingsService.getSetting('defaultVault');
39
40
					/**
41
					 * Using a native for loop for preformance reasons.
42
					 * More info see http://stackoverflow.com/questions/13843972/angular-js-break-foreach
43
					 */
44
					for (var i = 0; i < vaults.length; i++) {
45
						var vault = vaults[i];
46
						if (vault.guid === default_vault.guid) {
47
							$scope.default_vault = true;
48
							$scope.list_selected_vault = vault;
49
							SettingsService.setSetting('defaultVault', vault);
50
							if (SettingsService.getSetting('defaultVaultPass')) {
51
								$location.path('/vault/' + vault.guid);
52
							}
53
							break;
54
						}
55
					}
56
				}
57
			});
58
59
60
			var key_strengths = [
61
				'password.poor',
62
				'password.poor',
63
				'password.weak',
64
				'password.good',
65
				'password.strong'
66
			];
67
68
			$scope.default_vault = false;
69
			$scope.remember_vault_password = false;
70
			$scope.auto_logout_timer = false;
71
			$scope.logout_timer = '0';
72
			$scope.list_selected_vault = false;
73
			$scope.minimal_value_key_strength = 3;
74
75
			$rootScope.$on('settings_loaded', function () {
76
				$scope.minimal_value_key_strength = SettingsService.getSetting('vault_key_strength');
77
				$translate(key_strengths[SettingsService.getSetting('vault_key_strength')]).then(function(translation){
78
					$scope.required_score = {'strength': translation};
79
				});
80
81
			});
82
83
			$scope.toggleDefaultVault = function () {
84
				$scope.default_vault = !$scope.default_vault;
85
				if ($scope.default_vault === true) {
86
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
87
				} else {
88
					SettingsService.setSetting('defaultVault', null);
89
				}
90
			};
91
92
			$scope.toggleRememberPassword = function () {
93
				$scope.remember_vault_password = !$scope.remember_vault_password;
94
				if ($scope.remember_vault_password) {
95
					SettingsService.setSetting('defaultVault', $scope.list_selected_vault);
96
					$scope.default_vault = true;
97
				}
98
				if ($scope.remember_vault_password !== true) {
99
					SettingsService.setSetting('defaultVault', null);
100
				}
101
			};
102
103
			$scope.toggleAutoLogout = function(){
104
				$scope.auto_logout_timer = !$scope.auto_logout_timer;
105
			};
106
107
			$scope.clearState = function () {
108
				$scope.list_selected_vault = false;
109
				$scope.creating_vault = false;
110
				$scope.error = false;
111
			};
112
113
			$scope.selectVault = function (vault) {
114
				$scope.list_selected_vault = vault;
115
			};
116
			$scope.sharing_keys = {};
117
			$scope.newVault = function () {
118
				$scope.creating_vault = true;
119
				var key_size = 1024;
120
				ShareService.generateRSAKeys(key_size).progress(function (progress) {
121
					var p = progress > 0 ? 2 : 1;
122
					var msg = $translate.instant('generating.sharing.keys');
123
					msg = msg.replace('%step', p);
124
					$scope.creating_keys = msg;
125
					$scope.$digest();
126
				}).then(function (kp) {
127
					var pem = ShareService.rsaKeyPairToPEM(kp);
128
					$scope.creating_keys = false;
129
					$scope.sharing_keys.private_sharing_key = pem.privateKey;
130
					$scope.sharing_keys.public_sharing_key = pem.publicKey;
131
					$scope.$digest();
132
				});
133
134
			};
135
136
			var _loginToVault = function (vault, vault_key) {
137
				var _vault = angular.copy(vault);
138
				_vault.vaultKey = angular.copy(vault_key);
139
				delete _vault.credentials;
140
				var timer =  parseInt($scope.logout_timer);
141
				if($scope.auto_logout_timer && timer > 0 ){
142
					$rootScope.$broadcast('logout_timer_set', timer*60);
143
				}
144
145
				VaultService.setActiveVault(_vault);
146
				$location.path('/vault/' + vault.guid);
147
			};
148
149
			$scope.selectLogoutTimer = function (time) {
150
				$scope.auto_logout_timer = true;
151
				$scope.logout_timer = time;
152
			};
153
154
			$scope.vaultDecryptionKey = '';
155
			$scope.loginToVault = function (vault, vault_key) {
156
				$scope.error = false;
157
				var _vault = angular.copy(vault);
158
				_vault.vaultKey = angular.copy(vault_key);
159
160
				VaultService.setActiveVault(_vault);
161
				try {
162
					EncryptService.decryptString(vault.challenge_password);
163
					if ($scope.remember_vault_password) {
164
						SettingsService.setSetting('defaultVaultPass', vault_key);
165
					}
166
					_loginToVault(vault, vault_key);
167
168
				} catch (e) {
169
					$scope.error = $translate.instant('invalid.vault.key');
170
				}
171
172
			};
173
174
175
			$scope.createVault = function (vault_name, vault_key, vault_key2) {
176
				if (vault_key !== vault_key2) {
177
					$scope.error = $translate.instant('password.do.not.match');
178
					return;
179
				}
180
				VaultService.createVault(vault_name).then(function (vault) {
181
					$scope.vaults.push(vault);
182
					var _vault = angular.copy(vault);
183
					_vault.vaultKey = angular.copy(vault_key);
184
					VaultService.setActiveVault(_vault);
185
					SettingsService.setSetting('defaultVaultPass', null);
186
					SettingsService.setSetting('defaultVault', null);
187
					var test_credential = CredentialService.newCredential();
188
					test_credential.label = 'Test key for vault ' + vault_name;
189
					test_credential.hidden = true;
190
					test_credential.vault_id = vault.vault_id;
191
					test_credential.password = 'lorum ipsum';
192
					CredentialService.createCredential(test_credential).then(function () {
193
						_vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key);
194
						_vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key));
195
						VaultService.updateSharingKeys(_vault).then(function () {
196
							_loginToVault(vault, vault_key);
197
						});
198
					});
199
				});
200
			};
201
		}]);
202
}());